package com.example.feigner;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.AbstractClientHttpResponse;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;

/**
 * @author cxx
 * @create 2020-01-15 23:54
 **/
public class SimpleClientHttpResponse extends AbstractClientHttpResponse {

    private URLConnection urlConnection;

    public SimpleClientHttpResponse(URLConnection urlConnection) {
        this.urlConnection=urlConnection;
    }


    /**
     * Return the HTTP status code (potentially non-standard and not
     * resolvable through the {@link HttpStatus} enum) as an integer.
     *
     * @return the HTTP status as an integer value
     * @throws IOException in case of I/O errors
     * @see #getStatusCode()
     * @see HttpStatus#resolve(int)
     * @since 3.1.1
     */
    @Override
    public int getRawStatusCode() throws IOException {
        return 200;
    }

    /**
     * Return the HTTP status text of the response.
     *
     * @return the HTTP status text
     * @throws IOException in case of I/O errors
     */
    @Override
    public String getStatusText() throws IOException {
        return "ok";
    }

    /**
     * Close this response, freeing any resources created.
     */
    @Override
    public void close() {

    }

    /**
     * Return the body of the message as an input stream.
     *
     * @return the input stream body (never {@code null})
     * @throws IOException in case of I/O errors
     */
    @Override
    public InputStream getBody() throws IOException {
        return urlConnection.getInputStream ();
    }

    /**
     * Return the headers of this message.
     *
     * @return a corresponding HttpHeaders object (never {@code null})
     */
    @Override
    public HttpHeaders getHeaders() {
        return new HttpHeaders();
    }
}
